(*--------------------------------------------------------
    Holt                  http://coderesearchlabs.com/holt
    by Javier Santo Domingo (j-a-s-d@coderesearchlabs.com)
  --------------------------------------------------------
    core
  --------------------------------------------------------*)

unit holt_core;

{$I holt_defines.inc}

interface

uses
  holt_architecture,
  holt_types;

function NewCore: IhCore;

implementation

uses
  holt_processor,
  holt_tree,
  holt_reporting,
  holt_emission,
  holt_consts;

{ ThCore }

type
  ThCore = class(TInterfacedObject, IhCore)
  strict private
    FProcessor: IhProcessor;
    FTree: IhTree;
    FReportBuilder: IhReportBuilder;
    FEmissionController: IhEmissionController;
    function ExecuteCommandsFile(const AFilename: gString): gBoolean;
  strict protected
    procedure RunBatchMode(const AParameter: gString);
    procedure RunInteractiveMode;
  public
    procedure AfterConstruction; override;
  end;

function ThCore.ExecuteCommandsFile(const AFilename: gString): gBoolean;
begin
  result := FProcessor.Execute(
    FEmissionController, FReportBuilder, FTree, OPCODE_EXECUTE + ' ' + AFilename
  );
end;

procedure ThCore.RunBatchMode(const AParameter: gString);
begin
  ExecuteCommandsFile(AParameter);
  if FProcessor.GetLastResult <> '' then
    write(FProcessor.GetLastResult);
end;

procedure ThCore.RunInteractiveMode;

  function nextCommand: gString; inline;
  begin
    write(FReportBuilder.AsPrompt(FTree.GetPath));
    readln(result);
  end;

begin
  writeln(TITLE);
  while FProcessor.Execute(FEmissionController, FReportBuilder, FTree, nextCommand) do
    writeln(FProcessor.GetLastResult);
end;

procedure ThCore.AfterConstruction;
begin
  inherited;
  FProcessor := NewProcessor;
  FTree := NewTree;
  FEmissionController := NewEmissionController;
  FReportBuilder := NewReportBuilder;
  ExecuteCommandsFile(FILE_INIT);
end;

function NewCore: IhCore;
begin
  result := ThCore.Create;
end;

end.
